package com.example.exceldemo.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.example.exceldemo.entity.TUser;
import com.example.exceldemo.mapper.TUserMapper;
import com.example.exceldemo.mapper.UserMapper;
import com.example.exceldemo.service.ExportService;
import com.example.exceldemo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * @Author: yanhongwei
 * @Date: 2023/07/13 14:41:41
 * @Description: TODO
 */
@RestController
@RequestMapping("/excel")
@Api(tags = "excel导出")
public class WriteController {

    @Autowired
    UserMapper userMapper;

    @Autowired
    UserService userService;

    @Autowired
    TUserMapper tUserMapper;

    @Autowired
    ExportService exportService;

    @GetMapping("/exportExcel01")
    @ApiOperation("导出到某地址")
    public String exportExcel() {
        //1 设置写入文件夹地址和excel文件名称
        String filename = "C:\\Users\\admin\\Desktop\\t-user.xlsx";
        // 2 调用easyexcel里面的方法实现写操作
        // write方法两个参数：第一个参数文件路径名称，第二个参数实体类class
        long l1 = System.currentTimeMillis();
        EasyExcel.write(filename, TUser.class)
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                .sheet("学生列表").doWrite(tUserMapper.selectList(null));
        long l2 = System.currentTimeMillis();
        return "数据导出成功,花费时间" + (l2 - l1) + "毫秒";
    }

    /**
     * 导出数据字典到Excel
     *
     * @param response
     * @return
     */
    @GetMapping("/exportExcel02")
    @ApiOperation("web方式导出")
    public String exportDictData(HttpServletResponse response) {
        long s = System.currentTimeMillis();
        exportService.exportExcel02(response);
        long e = System.currentTimeMillis();
        double time = (e - s) / 1000;
        double haomiao = e - s;
        return "导出成功" + "耗时：" + time + "秒；" + haomiao + "毫秒";
    }

    /**
     * 导出数据字典到Excel
     *
     * @param response
     * @return
     */
    @GetMapping("/exportExcel03")
    @ApiOperation("下载模板")
    public void exportDictModel(HttpServletResponse response) {
        exportService.exportDictModel(response);
    }

    /**
     * 导出数据字典到Excel，使用多线程批量导出
     */
    @GetMapping("/batchExport")
    @ApiOperation("大数据批量导出到某地址")
    public String batchExport() {
        long s = System.currentTimeMillis();
        exportService.batchExport();
        long e = System.currentTimeMillis();
        double time = (e - s) / 1000;
        double haomiao = e - s;
        return "导出成功" + "耗时：" + time + "秒；" + haomiao + "毫秒";
    }

    /**
     * 导出数据字典到Excel，使用多线程批量导出
     *
     * @param response
     * @return
     */
    @GetMapping("/batchExportToWeb")
    @ApiOperation("web大数据批量导出")
    public void batchExport(HttpServletResponse response) {
        exportService.batchExport(response);
    }

}
